<template>
  <!-- <div> -->
  <header class="header">
    <!-- <div class="header__back" @click="goBack"></div> -->
    <div class="header-back" @click="goBack">
      <van-icon name="arrow-left" />
    </div>
    <!-- <div class="header-back" @click="goBack"><van-icon name="revoke" /></div> -->
    <div class="header__title">{{ title }}</div>
    <div
      class="header__right1"
      v-if="
        shows == false &&
        StandingWorkFullShow == false &&
        equipmentWorkFull == false &&
        obstacleWorkFull == false
      "
    ></div>
    <div class="header__right" v-if="shows" @click="submit">提交全部</div>
    <div
      class="header__right"
      v-if="StandingWorkFullShow"
      @click="StandingWorkFullsubmit"
    >
      提交
    </div>
    <div
      class="header__right"
      v-if="equipmentWorkFull"
      @click="sendequipmentWorkFullList"
    >
      提交全部
    </div>
    <div
      class="header__right"
      v-if="obstacleWorkFull"
      @click="submitObstacleWorkFullList"
    >
      提交全部
    </div>
    <div
      class="header__right"
      v-if="inspectionWorkFull"
      @click="submitInspectionWorkFullList"
    >
      提交全部
    </div>
    <div
      class="header__right"
      v-if="equipmentInstallWorkFull"
      @click="submitEquipmentInstallWorkFullList"
    >
      提交全部
    </div>
    <div
      class="header__right"
      v-if="systemInstallWorkFull"
      @click="submitSystemInstallWorkFullList"
    >
      提交全部
    </div>
    <div
      class="header__right"
      v-if="emergencyWorkFull"
      @click="submitEmergencyWorkFullList"
    >
      提交全部
    </div>

    <slot></slot>
  </header>
  <!-- </div> -->
</template>
<script>
import { compCollectAddComps, cusCollectUpdinfo, updInfo,automaticAcquisitionUpdInfo } from "../api/index";
import qs from "qs";
export default {
  props: {
    title: {
      type: String,
      default: "",
    },
    backFilg: {
      type: String,
      default: "",
    },
    shows: {
      type: Boolean,
      default: false,
    },
    StandingWorkFullShow: {
      type: Boolean,
      default: false,
    },
    equipmentWorkFull: {
      type: Boolean,
      default: false,
    },
    obstacleWorkFull: {
      type: Boolean,
      default: false,
    },
    tabs: {
      type: Array,
      default: () => [],
    },
    StandingWorkFullForm: {
      type: Object,
    },
    equipmentWorkFullList: {
      type: Object,
    },
    obstacleWorkFullList: {
      type: Object,
    },
    taskNo: {
      type: String,
    },
    danNo: {
      type: Number,
    },
    list: {
      type: Object,
    },
    inspectionWorkFullList: {
      type: Array,
    },
    inspectionWorkFull: {
      type: Boolean,
      default: false,
    },
    equipmentInstallWorkFullList: {
      type: Object,
    },
    equipmentInstallWorkFull: {
      type: Boolean,
      default: false,
    },
    systemInstallWorkFullList: {
      type: Object,
    },
    systemInstallWorkFull: {
      type: Boolean,
      default: false,
    },
    emergencyWorkFullList: {
      type: Object,
    },
    emergencyWorkFull: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    getFileFromUrl(url, fileName) {
      return new Promise((resolve, reject) => {
        var blob = null;
        var xhr = new XMLHttpRequest();
        xhr.open("GET", url);
        xhr.setRequestHeader("Accept", "image/png");
        xhr.responseType = "blob";
        // 加载时处理
        xhr.onload = () => {
          // 获取返回结果
          blob = xhr.response;
          let file = new File([blob], fileName, { type: "image/png" });
          // 返回结果
          resolve(file);
        };
        xhr.onerror = (e) => {
          reject(e);
        };
        // 发送
        xhr.send();
      });
    },
    urlToBase64(url) {
      return new Promise((resolve, reject) => {
        let image = new Image();
        image.onload = function () {
          let canvas = document.createElement("canvas");
          canvas.width = this.naturalWidth;
          canvas.height = this.naturalHeight;
          // 将图片插入画布并开始绘制
          canvas.getContext("2d").drawImage(image, 0, 0);
          // result
          let result = canvas.toDataURL("image/png");
          resolve(result);
        };
        // CORS 策略，会存在跨域问题https://stackoverflow.com/questions/20424279/canvas-todataurl-securityerror
        image.setAttribute("crossOrigin", "Anonymous");
        image.src = url;
        // 图片加载失败的错误处理
        image.onerror = () => {
          reject(new Error("urlToBase64 error"));
        };
      });
    },

    // 应急保障工单填写提交
    submitEmergencyWorkFullList() {
      let forms = this.$props.emergencyWorkFullList;
      console.log(forms, "+++系统安装及调试工单填写提交");
    },
    // 系统安装及调试工单填写提交
    submitSystemInstallWorkFullList() {
      let forms = this.$props.systemInstallWorkFullList;
      console.log(forms, "+++系统安装及调试工单填写提交");
    },
    // 设备安装及调试工单填写提交
    submitEquipmentInstallWorkFullList() {
      let forms = this.$props.equipmentInstallWorkFullList;
      console.log(forms, "+++设备安装及调试工单填写提交");
    },
    // 智慧巡检工单填写提交
    submitInspectionWorkFullList() {
      let projectList = this.$props.inspectionWorkFullList;
      console.log(typeof projectList, "+++智能巡检工单填写提交");
      // let forms={
      //   taskNo:this.$props.taskNo,
      // }
      var param = {
        sid: window.sid,
        cmd: "com.awspaas.user.apps.opm_automatic_acquisition_updInfo",
        taskNo: this.$props.taskNo,
        // collectInfo:JSON.stringify(projectList)
      };
        // collectInfo: projectList,
      var params = {
        // taskNo: this.$props.taskNo,
        // collectInfo:forms
        collectInfo:projectList
      };
      automaticAcquisitionUpdInfo(param, params).then((res) => {
        // this.$store.commit("hideLoading");
        // const { result, msg, data, id } = res;
        // this.$toast.success({
        //   message: msg,
        // });
        // let listShow = false;
        // this.$emit("transfer", listShow);
        // // this.$router.push({
        // //   name: "arrived",
        // //   params: { list:this.$props.list},
        // // });
      });
    },
    // 智能排障工单填写提交
    submitObstacleWorkFullList() {
      console.log(111);
      let forms = this.$props.obstacleWorkFullList;
      if (
        forms.attachRepairedSticker &&
        forms.fX3cgdlta &&
        forms.resultsDisposePhoto &&
        forms.treatmentMode
      ) {
        var param = {
          sid: window.sid,
          cmd: "com.awspaas.user.apps.opm_Repair_updInfo",
          taskNo: this.$props.taskNo,
        };
        var params = {
          collectInfo: forms,
        };
        console.log(params);
        this.$store.commit("showLoading");
        updInfo(param, params).then((res) => {
          this.$store.commit("hideLoading");
          const { result, msg, data, id } = res;
          this.$toast.success({
            message: msg,
          });

          this.$router.push({
            name: "arrived",
            params: { list: this.$props.list },
          });
        });
      } else {
        this.$toast({
          message: "请完善数据",
        });
      }
    },
    sendequipmentWorkFullList() {
      this.$store.commit("showLoading");
      let forms = this.$props.equipmentWorkFullList;
      // console.log(forms,'forms')
      // if(forms.peripheralTerminalPhoto.length>0){
      //   forms.peripheralTerminalPhoto.forEach((item,index) => {
      //     if(!item.content){
      //       this.urlToBase64(item.url).then(res => {
      //         // 转化后的base64图片地址
      //         // console.log('base64', res)
      //         item.fileContent=res.split(',')[1].replace(/\=/g,"equalsign")
      //       })
      //       item.fileName=item.name
      //       delete item.message;
      //       delete item.status;
      //       delete item.name;
      //       delete item.url;
      //       // this.getFileFromUrl(item.url,'图片1.png')
      //       //   .then((response)=>{
      //       //     console.log(response,"++response")
      //       //     })
      //       //   .catch((e)=>{
      //       //     console.error(e)
      //       //     });

      //     }else{
      //       item.fileName=item.file.name
      //       item.fileContent=item.content.split(',')[1].replace(/\=/g,"equalsign")
      //       delete item.file;
      //       delete item.content;
      //       delete item.message;
      //       delete item.status;
      //       // fileName:item.file.name,fileContent:(item.content.split(',')[1].replace(/\=/g,"equalsign"))
      //     }
      //   });
      // }
      // console.log(forms.peripheralTerminalPhoto,"+++forms.peripheralTerminalPhoto")
      console.log(this.$props.danNo, "danNo");
      var param = {
        sid: window.sid,
        cmd: "com.awspaas.user.apps.opm_equipmentFileEstablishment",
        // cmd: "com.awspaas.user.apps.opm_test232222222222222222222",
        // taskNo:'FWGD2023000170'
        taskNo: this.$props.taskNo,
        type: "03",
        deviceName: this.$props.danNo,
      };
      var params = {
        collectInfo: forms,
      };
      cusCollectUpdinfo(param, params).then((res) => {
        this.$store.commit("hideLoading");
        const { result, msg, data, id } = res;
        this.$toast.success({
          message: msg,
        });
        let listShow = false;
        this.$emit("transfer", listShow);
        // this.$router.push({
        //   name: "arrived",
        //   params: { list:this.$props.list},
        // });
      });
    },
    StandingWorkFullsubmit() {
      let forms = this.$props.StandingWorkFullForm;
      if (
        // forms.useUnitName &&  forms.latestCompletionTime &&
        forms.telephoneTerminal &&
        forms.peripheralTerminal &&
        //  forms.networkTquipment &&
        forms.networkEquipment &&
        forms.computerTerminal &&
        forms.integratedWiring &&
        forms.networkEnvironment &&
        forms.machineRoom
      ) {
        var param = {
          sid: window.sid,
          cmd: "com.awspaas.user.apps.opm_cusCollect_updInfo",
          collectInfo: JSON.stringify(forms),
          // taskNo:'FWGD2023000084'
          taskNo: this.$props.taskNo,
          type: "02",
        };
        console.log(param, "++param");
        cusCollectUpdinfo(param).then((res) => {
          const { result, msg, data, id } = res;

          this.$toast.success({
            message: msg,
          });
          let listShow = false;
          this.$emit("transfer", listShow);
          this.$router.push({
            name: "arrived",
            params: { list: this.$props.list },
          });
        });
      } else {
        this.$toast({
          message: "请填写完整数据",
        });
      }
    },
    submit() {
      console.log("submit", this.tabs);

      var param = {
        sid: window.sid,
        cmd: "com.awspaas.user.apps.opm_compCollect_addComps",
        collectInfo: JSON.stringify(this.tabs),
        // taskNo:'FWGD2023000240'
        taskNo: this.$props.taskNo,
        type: "01",
      };
      compCollectAddComps(param).then((res) => {
        const { result, msg, data, id } = res;

        if (msg === "操作成功~") {
          this.$toast.success({
            message: msg,
          });
          let listShow = false;
          this.$emit("transfer", listShow);
          this.$router.push({
            name: "arrived",
            params: { list: this.$props.list },
          });
        }
        if (result === "error") {
          this.$toast({
            message: msg,
          });
        }
      });
    },
    goBack() {
      if (this.backFilg) {
        this.$emit("goBack");
      } else {
        this.$router.go(-1);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.header {
  height: 44px;
  // background: rgba(23, 119, 255, 1);
  // background: rgba(232, 57, 13, 1);
  background: #5290ff;
  //old
  position: relative;
  //  padding: 10px 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .header-back {
    margin-left: 16px;
  }
  .van-icon {
    //    color: #1A1A1A;
    color: #ffffff;
    font-size: 20px !important;
  }
  //  &__back {
  //   width: 10px;
  //   height: 18px;
  //   background-image: url(../assets/header/back.png);
  //   background-size: cover;
  //   position: absolute;
  //   left: 16px;
  //   &::after {
  //    content: "";
  //    display: block;
  //    width: 30px;
  //    height: 30px;
  //    position: absolute;
  //    left: -10px;
  //    top: -6px;
  //    background: transparent;
  //    z-index: 2;
  //   }
  //  }

  &__title {
    // font-family: PingFangSC, PingFangSC-Medium;
    font-family: "SimHei";
    color: rgba(255, 255, 255, 1);
    font-size: 18px;
    //   font-weight: 700;
    text-align: center;
    //   color: #1A1A1A;
    line-height: 20px;

    color: rgba(255, 255, 255, 1);
    font-size: 16px;
    font-weight: 500;
  }
  &__right {
    font-size: 12px;
    /* width: 100%; */
    background: #05ff97;
    color: #ffffff;
    padding: 4px 4px;
    margin-right: 10px;
  }
  &__right1 {
    font-size: 12px;
    /* width: 100%; */

    color: #ffffff;
    padding: 4px 4px;
    margin-right: 10px;
  }
}
</style>
